#include "../comm/httplib.h"
#include"oj_control.hpp"
#include<signal.h>
#include"oj_model.hpp"

using namespace httplib;
using namespace ns_control;
static Control* ctrl_ptr;
void recover(int signo)
{
    ctrl_ptr->RecoveryMachine();
    
}



int main()
{
   
    Server svr;
    Control ctrl;
    ctrl_ptr=&ctrl;

    signal(2,recover);
    INFO_Log("开始启动");
    svr.Get("/all_questions", [&ctrl](const Request &req, Response &resp)
        {       
                std::string html;
                ctrl.AllQuestions(&html);
                INFO_Log("获取到题目列表");
                resp.set_content(html, "text/html; charset=utf-8"); 
        }
        
        );

    svr.Get(R"(/question/(\d+))", [&ctrl](const Request &req, Response &resp)
            {
            std::string number = req.matches[1];
            std::string html;
            INFO_Log("获取到单个题目");
            ctrl.Question(number,&html);
            resp.set_content(html, "text/html; charset=utf-8"); 
            });

    svr.Post(R"(/judge/(\d+))", [&ctrl](const Request &req, Response &resp)
            {
            std::string number = req.matches[1];
        //     resp.set_content("指定题目的判题: " + number, "text/plain; charset=utf-8");
        INFO_Log("进行题目判断：%s",number.c_str());
        std::string result_json;
        ctrl.Judge(number, req.body, &result_json);
        resp.set_content(result_json, "application/json;charset=utf-8");
            });



    svr.set_base_dir("./wwwroot/");
    svr.listen("0.0.0.0", 8888);
    return 0;

     

}